export ARCH=x86

PREFIX = i686-elf-
QEMU = qemu-system-i386
QEMU_ARCH_FLAG = -M q35
ifeq ($(ARCH),riscv)
PREFIX = riscv32-elf-
QEMU = qemu-system-riscv32
QEMU_ARCH_FLAG = -M virt
endif

export CC= $(PREFIX)gcc
export CFLAGS= -fno-strict-aliasing -O2 -Wall -Wextra -std=c17 -fno-omit-frame-pointer \
		-fno-stack-protector -gdwarf-5 -Werror-implicit-function-declaration
export CXX= $(PREFIX)g++
export CXXFLAGS= -fno-strict-aliasing -O2 -Wall -Wextra -std=c++20 -fno-omit-frame-pointer -fno-stack-protector \
				-gdwarf-5 -fno-sized-deallocation -fno-exceptions -fno-rtti -fno-use-cxa-atexit -Werror-implicit-function-declaration
export ASFLAGS = -gdwarf-5
export AS= $(PREFIX)as
ifeq ($(ARCH),x86)
ASFLAGS += -Wa,-divide
endif
export LD= $(PREFIX)ld
export OBJCOPY= $(PREFIX)objcopy
export AR = $(PREFIX)ar

ifeq ($(ARCH),x86)
USERSPACE_MODULE_TARGET = program module
endif

all: panicos.img

usb: panicos.img
	sudo dd if=panicos.img of=/dev/sdb bs=4M conv=fsync,nocreat

# qemu
# 开一个干净的子 shell，完全丢掉 Snap 相关的环境变量
env -i \
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
  LD_LIBRARY_PATH="" \
  DISPLAY=":1" \
  XDG_SESSION_TYPE="x11" \
  bash --norc --noprofile

qemu: panicos.img
	$(QEMU) -debugcon mon:stdio -kernel kernel/kernel -drive file=panicos.img,format=raw,if=virtio \
	-smp 2 -m 128M -net none -rtc base=localtime $(QEMU_ARCH_FLAG)

qemu-gdb: panicos.img
	$(QEMU) -debugcon mon:stdio -kernel kernel/kernel -drive file=panicos.img,format=raw,if=virtio \
	-smp 2 -m 128M -s -S -net none -rtc base=localtime$(QEMU_ARCH_FLAG)

qemu-kvm: panicos.img
	$(QEMU) -debugcon mon:stdio -kernel kernel/kernel -drive file=panicos.img,format=raw,if=virtio \
	-smp 2 -m 128M -accel kvm -cpu host -net none -rtc base=localtime $(QEMU_ARCH_FLAG)

panicos.img: boot/mbr.bin kernel/kernel rootfs share $(USERSPACE_MODULE_TARGET)
	dd if=/dev/zero of=panicos.img bs=1M count=64
	sudo parted panicos.img mklabel msdos
	yes Ignore | sudo parted panicos.img mkpart primary fat32 1 32
	sudo losetup -P /dev/loop17 panicos.img
	sudo /sbin/mkfs.vfat -F32 -s1 /dev/loop17p1
	sudo mkdir -p /mnt/panicos
	sudo mount /dev/loop17p1 /mnt/panicos
	sudo cp -r rootfs/* /mnt/panicos
	sudo grub-install --target=i386-pc --boot-directory=/mnt/panicos/boot --no-floppy /dev/loop17
	echo 'set timeout=3' | sudo tee /mnt/panicos/boot/grub/grub.cfg
	echo 'menuentry "panicos" {' | sudo tee -a /mnt/panicos/boot/grub/grub.cfg
	echo '	insmod part_msdos        # 如果硬盘是 MBR' | sudo tee -a /mnt/panicos/boot/grub/grub.cfg
	echo '	insmod fat              # 根分区文件系统' | sudo tee -a /mnt/panicos/boot/grub/grub.cfg
	echo '	set root="hd0,msdos1"    # 根据实际情况改' | sudo tee -a /mnt/panicos/boot/grub/grub.cfg
	echo '	multiboot /boot/kernel' | sudo tee -a /mnt/panicos/boot/grub/grub.cfg
	echo '}' | sudo tee -a /mnt/panicos/boot/grub/grub.cfg
	sudo umount /mnt/panicos
	sudo losetup -d /dev/loop17

boot/mbr.bin:
	$(MAKE) -C boot mbr.bin

kernel/kernel: rootfs
	$(MAKE) -C kernel kernel
	cp kernel/kernel rootfs/boot

.PHONY: program
program: library rootfs
	$(MAKE) -C program

.PHONY: library
library: rootfs
	$(MAKE) -C library

.PHONY: share
share: rootfs
	$(MAKE) -C share install

.PHONY: module
module: rootfs
	$(MAKE) -C module

.PHONY: rootfs
rootfs:
	mkdir -p rootfs/bin rootfs/lib rootfs/devel/include rootfs/devel/lib \
	rootfs/share rootfs/boot/module

.PHONY: dist
dist: kernel/kernel rootfs share $(USERSPACE_MODULE_TARGET)
	tar -czf panicos.tar.gz rootfs/*

.PHONY: clean
clean:
	$(MAKE) -C boot clean
	$(MAKE) -C kernel clean
	$(MAKE) -C library clean
	$(MAKE) -C program clean
	$(MAKE) -C module clean
	rm -rf panicos.img rootfs panicos.tar.gz
